import { plainToInstance } from "class-transformer";
import ResultHelper from "../routes/utils/ResultHelper";
import { JournalsEntity } from "../entities/JournalsEntity";
import JournalDao from "../dao/JournalDao";

export default class JournalService {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  static async add(body: any) {
    if (!body.content) {
      return ResultHelper.error("请输入内容");
    }

    try {
      const Journal = plainToInstance(JournalsEntity, body);
      const errs = await Journal.validateThie();
      if (errs.length != 0) {
        return ResultHelper.error(errs);
      }

      const result = await JournalDao.add(Journal);
      if (JSON.stringify(result) !== "{}") {
        return ResultHelper.success(result, "发布成功");
      } else {
        return ResultHelper.error("发布失败");
      }
    } catch ({ message }) {
      return ResultHelper.error(message);
    }
  }

  static async del(uid: number, id: number) {
    if (!uid) {
      return ResultHelper.error("没有用户标识");
    }

    if (!id) {
      return ResultHelper.error("没有journal标识");
    }

    try {
      const result = await JournalDao.del(uid, id);

      return result
        ? ResultHelper.success({}, "删除成功")
        : ResultHelper.error("删除失败");
    } catch ({ message }) {
      return ResultHelper.error(message);
    }
  }

  static async findByPage(uid: number) {
    if (!uid) {
      return ResultHelper.error("没有用户标识");
    }

    try {
      const result = await JournalDao.findByPage(uid);
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      result.map((item: any) => {
        item.photos != "" && (item.photos = JSON.parse(item.photos));
        item.ext_data != "" && (item.ext_data = JSON.parse(item.ext_data));
      });

      return ResultHelper.success(result);
    } catch ({ message }) {
      return ResultHelper.error(message);
    }
  }

  static async findByUidAndMid(uid: number, mid: number) {
    if (!uid) {
      return ResultHelper.error("没有用户标识");
    }

    if (!mid) {
      return ResultHelper.error("没有 marker 标识");
    }

    try {
      const result = await JournalDao.findByUidAndMidOmit(uid, mid);
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      result.map((item: any) => {
        item.photos != "" && (item.photos = JSON.parse(item.photos)[0]);
        item.ext_data != "" && (item.ext_data = JSON.parse(item.ext_data));
      });

      return ResultHelper.success(result);
    } catch ({ message }) {
      return ResultHelper.error(message);
    }
  }

  static async findById(uid: number, id: number) {
    if (!uid) {
      return ResultHelper.error("没有用户标识");
    }

    if (!id) {
      return ResultHelper.error("没有日志标识");
    }

    try {
      const result = await JournalDao.findById(uid, id);

      if (result === null) {
        return ResultHelper.error("没有找到日志");
      } else {
        result.photos != "" &&
          (result.photos = JSON.parse(result.photos as string));
        result.ext_data != "" &&
          (result.ext_data = JSON.parse(result.ext_data as string));
      }

      return ResultHelper.success(result);
    } catch ({ message }) {
      return ResultHelper.error(message);
    }
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  static async modify(body: any) {
    if (!body.u_id) {
      return ResultHelper.error("没有用户标识");
    }

    if (!body.id) {
      return ResultHelper.error("没有日志标识");
    }

    const Journal = plainToInstance(JournalsEntity, body);
    const errs = await Journal.validateThie(true);

    if (errs.length > 0) {
      return ResultHelper.error(errs);
    }

    try {
      const result = await JournalDao.modify(Journal);

      return result
        ? ResultHelper.success({}, "修改成功")
        : ResultHelper.error("修改失败");
    } catch ({ message }) {
      return ResultHelper.error(message);
    }
  }

  /**
   * 查找用户公开的日志
   * @param body
   * @returns
   */
  static async findOpenJournalList(body: { u_id: number; m_id: number }) {
    if (!body.u_id) {
      return ResultHelper.error("没有用户标识");
    }

    if (!body.m_id) {
      return ResultHelper.error("没有日志标识");
    }

    try {
      const result = await JournalDao.findOpenJournalList(body.u_id, body.m_id);

      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      result.map((item: any) => {
        item.photos != "" && (item.photos = JSON.parse(item.photos)[0]);
        item.ext_data != "" && (item.ext_data = JSON.parse(item.ext_data));
      });

      return ResultHelper.success(result);
    } catch ({ message }) {
      return ResultHelper.error(message);
    }
  }

  static async findOneOpenJournal(body: {
    u_id: number;
    id: number;
    m_id: number;
  }) {
    const { u_id, id, m_id } = body;

    if (!u_id) {
      return ResultHelper.error("没有用户标识");
    }

    if (!id) {
      return ResultHelper.error("没有日志标识");
    }

    if (!m_id) {
      return ResultHelper.error("没有 marker 标识");
    }

    try {
      const result = await JournalDao.findOneOpenJournal(u_id, m_id, id);

      if (result === null) {
        return ResultHelper.error("没有找到日志");
      } else {
        result.photos != "" &&
          (result.photos = JSON.parse(result.photos as string));
        result.ext_data != "" &&
          (result.ext_data = JSON.parse(result.ext_data as string));
      }

      return ResultHelper.success(result);
    } catch ({ message }) {
      return ResultHelper.error(message);
    }
  }
}
